import scrapy
import pandas as pd
from ..items import FjmoviesItem
'''
爬取信息的数据结构
名字
相应url
最新集名
图片url
'''
class FjmsSpider(scrapy.Spider):
    name = 'fjms'
    allowed_domains = ['feijisu02.com']
    url = 'http://feijisu02.com/acg/2021/0/all/{}.html'
    offset = 0
    # 首次运行时 start_requests 方法返回 scrapy.Request 对象->parse
    def start_requests(self):
        for page in range(0,20):
            url=self.url.format(page)
            yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        # 解析请求返回的内容
        items = FjmoviesItem()
        lists = response.xpath('//li[@class="item"]')
        for i in lists:
            items['name'] = i.xpath('./a/@title').get()
            items['latest_load'] = i.xpath('./a/div[1]/span').get()
            items['url'] = self.allowed_domains[0]+i.xpath('./a/@href').get()
            items['img_src'] = i.xpath('./a/div[1]/img/@data-img').get()
            # 对item封装数据后，就调用yield把控制权给管道，管道拿到处理后return返回，又回到该程序。
            yield items


